package cate.game.role.res.channelgift.core;

import cate.common.table.d.GDOperation;
import cate.common.table.res.channelgift.ChannelGiftBaseRow;
import cate.common.util.TimeTool;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.mail.Mail;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

public class SingleChannelGiftPO extends RoleDependent {
    @NoteField("配置ID")
    public int tid;
    @NoteField("已领取数量")
    public int gotNum;
    @NoteField("上次领取的时间")
    public long lastGotTime;

    public SingleChannelGiftPO() {

    }

    public SingleChannelGiftPO(int tid, Role role) {
        this.tid = tid;
        initialize(role);
    }

    @JsonIgnore
    private ChannelGiftBaseRow getConfig() {
        return role.getGame().table.channelGift.base.get(tid);
    }

    /**
     * 清空数据
     */
    private void clear() {
        gotNum = 0;
        lastGotTime = 0;
    }

    /**
     * 领取奖励
     */
    public EcResult<Void> receiveGift() {
        EcResult<Void> r = new EcResult<>();
        ChannelGiftBaseRow config = getConfig();
        if (config == null) {
            return r.fail("礼包不存在哦");
        }
        if (gotNum >= config.limit) {
            return r.fail("礼包领完了哦");
        }
        long nowTime = System.currentTimeMillis();
        if (lastGotTime > 0 && config.gapMinute > 0 && nowTime - lastGotTime < config.gapMinute * TimeTool.TimeCons.MINUTE) {
            return r.fail("礼包领取还在冷却中哦");
        }
        MixRes reward = new MixRes(config.rewardStr);
        if (reward.isEmpty()) {
            return r.fail("礼包奖励配置错了哦");
        }
        gotNum++;
        lastGotTime = nowTime;
        if (config.mailId > 0) {
            Mail mail = new MailBuilder().setReward(reward)
                    .setContentId(config.mailId)
                    .setOperation(GDOperation.CHANNEL_GIFT)
                    .setOperationSub(tid)
                    .build();
            role.getMail().add(mail);
        } else {
            new MixResAdder().setRes(reward)
                    .setDialogReward(true)
                    .setSendMailWhenFull(true)
                    .setOperation(GDOperation.CHANNEL_GIFT)
                    .setOperationSub(tid)
                    .exe(role);
        }
        return r.success();
    }

    /**
     * 根据类型重置
     */
    public void resetByType(int type) {
        ChannelGiftBaseRow config = getConfig();
        if (config == null) {
            return;
        }
        if (config.resetType == type) {
            clear();
        }
    }

    @Override
    public void onDaySpan(boolean silence) {
        resetByType(1);
    }

    @Override
    public void onWeekSpan(boolean silence) {
        resetByType(2);
    }

    @Override
    public void onMonthSpan(boolean silence) {
        resetByType(3);
    }
}
